package cn.agiledata.bank.common.tag;

import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.bean.WriteTag;

import cn.agiledata.bank.common.util.ConvertUtil;
import cn.agiledata.bank.common.util.StringUtil;

public class ToUtfTag extends WriteTag {
	private String value;
	protected boolean filter = false;
	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
        // Look up the requested bean (if necessary)
		Object value = null;
		if(StringUtil.isStrEmpty(this.value)) {
	        if (ignore) {
	            if (TagUtils.getInstance().lookup(pageContext, name, scope) == null) {
	                return (SKIP_BODY); // Nothing to output
	            }
	        }

	        // Look up the requested property value
	        value = TagUtils.getInstance().lookup(pageContext, name, property, scope);			
		}


        if (this.value == null && value == null) {
            return (SKIP_BODY); // Nothing to output
        }
        String output = "";
        // Convert value to the String with some formatting
        if(!StringUtil.isStrEmpty(this.value)) {
        	output = formatValue(this.value);
        }
        if(value != null) {
            if(!StringUtil.isStrEmpty(value.toString())) {
            	output = formatValue(value);
            }        	
        }

        // Print this property value to our output writer, suitably filtered
        if (filter) {
            TagUtils.getInstance().write(pageContext, TagUtils.getInstance().filter(output));
        } else {
            TagUtils.getInstance().write(pageContext, output);
        }

        // Continue processing this page
        return (SKIP_BODY);		
	}

	protected String formatValue(Object valueToFormat) throws JspException {
		// TODO Auto-generated method stub
		String string = valueToFormat.toString();
		string = ConvertUtil.toUTF(string);
		return super.formatValue(string);
	}

}
